import request from '@/config/axios'

// ========== 人脸配置管理 ==========

export interface FaceConfigVO {
  id: number
  name: string
  appId: string
  apiKey: string
  secretKey: string
  groupId: number
  threshold: number
  enabled: boolean
  status: number
  remark?: string
  createTime: Date
}

export interface FaceConfigPageReqVO extends PageParam {
  name?: string
  status?: number
  createTime?: Date[]
}

export interface FaceConfigSaveReqVO {
  id?: number
  name: string
  appId: string
  apiKey: string
  secretKey: string
  groupId: number
  threshold: number
  enabled: boolean
  status: number
  remark?: string
}

// 获取人脸配置分页
export const getFaceConfigPage = (params: FaceConfigPageReqVO) => {
  return request.get({ url: '/infra/face-config/page', params })
}

// 获取人脸配置详情
export const getFaceConfig = (id: number) => {
  return request.get({ url: '/infra/face-config/get?id=' + id })
}

// 新增人脸配置
export const createFaceConfig = (data: FaceConfigSaveReqVO) => {
  return request.post({ url: '/infra/face-config/create', data })
}

// 修改人脸配置
export const updateFaceConfig = (data: FaceConfigSaveReqVO) => {
  return request.put({ url: '/infra/face-config/update', data })
}

// 删除人脸配置
export const deleteFaceConfig = (id: number) => {
  return request.delete({ url: '/infra/face-config/delete?id=' + id })
}

// 获取启用的人脸配置
export const getFaceConfigSimpleList = () => {
  return request.get({ url: '/infra/face-config/get-enabled' })
}

// ========== 人脸识别 ==========

export interface FaceRegisterReqVO {
  configId: number
  userId: string
  userName: string
  groupId: string
  image: string // base64编码的图片
  userInfo?: string
}

export interface FaceSearchReqVO {
  configId: number
  groupId: string
  image: string // base64编码的图片
  maxUserNum?: number
}

export interface FaceDetectReqVO {
  configId: number
  image: string // base64编码的图片
  faceField?: string
}

export interface FaceRegisterRespVO {
  faceToken: string
  location: {
    left: number
    top: number
    width: number
    height: number
    rotation: number
  }
}

export interface FaceSearchRespVO {
  faceToken: string
  userList: {
    groupId: string
    userId: string
    userName: string
    score: number
    userInfo: string
  }[]
}

export interface FaceDetectRespVO {
  faceNum: number
  faceList: {
    faceToken: string
    location: {
      left: number
      top: number
      width: number
      height: number
      rotation: number
    }
    faceFields: any
  }[]
}

// 人脸注册
export const faceRegister = (data: FaceRegisterReqVO) => {
  return request.post({ url: '/infra/face/register', data })
}

// 人脸搜索
export const faceSearch = (data: FaceSearchReqVO) => {
  return request.post({ url: '/infra/face/search', data })
}

// 人脸检测
export const faceDetect = (data: FaceDetectReqVO) => {
  return request.post({ url: '/infra/face/detect', data })
}

// 删除用户人脸
export const deleteFaceUser = (configId: number, userId: string, groupId: string) => {
  return request.delete({ 
    url: '/infra/face/delete-user', 
    params: { configId, userId, groupId } 
  })
}

// 删除人脸
export const deleteFace = (data: any) => {
  return request.delete({ url: `/infra/face/delete`, data })
}

// ========== Base64 版本的人脸识别接口 ==========

// 人脸注册 - Base64版本
export const registerFaceBase64 = (data: any) => {
  return request.post({ url: `/infra/face/register-base64`, data })
}

// 人脸搜索 - Base64版本
export const searchFaceBase64 = (data: any) => {
  return request.post({ url: `/infra/face/search-base64`, data })
}

// 人脸检测 - Base64版本
export const detectFaceBase64 = (data: any) => {
  return request.post({ url: `/infra/face/detect-base64`, data })
}

// 获取用户人脸列表
export const getFaceUserList = (configId: number, groupId: string) => {
  return request.get({ 
    url: '/infra/face/user-list', 
    params: { configId, groupId } 
  })
}

// ========== 人脸记录管理 ==========

export interface FaceRecordVO {
  id: number
  configId: number
  configName: string
  type: number // 1-注册 2-搜索 3-检测
  userId?: string
  userName?: string
  groupId?: string
  image: string
  result: string
  status: number // 1-成功 2-失败
  errorMsg?: string
  createTime: Date
}

export interface FaceRecordPageReqVO extends PageParam {
  configId?: number
  type?: number
  userId?: string
  userName?: string
  groupId?: string
  status?: number
  createTime?: Date[]
}

// 获取人脸记录分页
export const getFaceRecordPage = (params: FaceRecordPageReqVO) => {
  return request.get({ url: '/infra/face-record/page', params })
}

// 获取人脸记录详情
export const getFaceRecord = (id: number) => {
  return request.get({ url: '/infra/face-record/get?id=' + id })
}

// 删除人脸记录
export const deleteFaceRecord = (id: number) => {
  return request.delete({ url: '/infra/face-record/delete?id=' + id })
}

// ========== 人脸日志管理 ==========

export interface FaceLogVO {
  id: number
  configId: number
  configName: string
  operation: string
  operationDetail: string
  userId?: string
  userName?: string
  ip: string
  userAgent: string
  status: number // 1-成功 2-失败
  errorMsg?: string
  createTime: Date
}

export interface FaceLogPageReqVO extends PageParam {
  configId?: number
  operation?: string
  userId?: string
  userName?: string
  status?: number
  createTime?: Date[]
}

// 获取人脸日志分页
export const getFaceLogPage = (params: FaceLogPageReqVO) => {
  return request.get({ url: '/infra/face-log/page', params })
}

// 获取人脸日志详情
export const getFaceLog = (id: number) => {
  return request.get({ url: '/infra/face-log/get?id=' + id })
}

// 删除人脸日志
export const deleteFaceLog = (id: number) => {
  return request.delete({ url: '/infra/face-log/delete?id=' + id })
}

// 清空人脸日志
export const clearFaceLog = () => {
  return request.delete({ url: '/infra/face-log/clear' })
}

// ==================== 导出别名 ====================

// 为了兼容组件中的导入，添加别名导出
export const detectFace = faceDetect
export const registerFace = faceRegister
export const searchFace = faceSearch